#include "Shield.h"

using namespace cocos2d;

bool Shield::init(){  
    if (!Creature::init()) {  
        return false;  
    }  

	// init
	d_objType = OBJ_TYPE_SHIELD;
	d_hasHpBar = false;

	int x = 300;//todo
	//this->setPosition(ccp(x, d_fieldSize.height/2));
	this->setPosition(ccp(x, d_winSize.height/2));

	d_creatureSprite = CCSprite::create("shield.png");
	ccBlendFunc blendFunc = {GL_SRC_ALPHA, GL_ONE}; 
	d_creatureSprite->setBlendFunc(blendFunc);

	this->setActive(true);

    return true;  
} 

bool Shield::hit(float damage)
{
	// hit animation
    d_creatureSprite->setColor(ccc3(0, 128, 255));
	if(d_actionTintTag != kCCActionTagInvalid) {
		d_creatureSprite->stopActionByTag(d_actionTintTag);
	}
	CCAction* action = CCTintTo::create(0.5f, 255, 255, 255);
	d_actionTintTag = action->getTag();
	d_creatureSprite->runAction(action);

	return false;
}

void Shield::setActive(bool active)
{
	d_active = active;
	if(d_active) {
		this->setVisible(true);
		d_creatureSprite->setColor(ccc3(255, 255, 255));
		d_creatureSprite->stopAllActions();
		d_creatureSprite->runAction(CCScaleTo::create(0.2f, 1.0f, 1.0f));
	} else {
		//this->setVisible(false);
		d_creatureSprite->stopAllActions();
		d_creatureSprite->runAction(CCScaleTo::create(0.2f, .0f, .0f));
	} 

}